package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Customer;
import pojo.FllowCase;

import com.alibaba.fastjson.JSON;
import common.JSONUtils;
import common.PinYinUtils;

import dao.CustomerDAO;
import dao.DAOFactory;
import dao.FllowCaseDAO;

@WebServlet("/action/getcustomer")
public class GetCustomersAction extends HttpServlet {
	private static final long serialVersionUID = -2390537734209442730L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String operatorId = req.getParameter("operatorid");
		if (operatorId.isEmpty()) {
			return;
		} else {
			CustomerDAO customerDAO = DAOFactory.getCustomerDAO();
			ArrayList<Customer> customers = (ArrayList<Customer>) customerDAO
					.getCustomersByOperatorId(operatorId);
			resp.setContentType("text/html;charse=UTF-8");
			PrintWriter out = resp.getWriter();
			String json = JSON.toJSONString(customers);
			out.println(json);
			out.flush();
			out.close();
		}
	}

	public static void main(String[] args) {
		CustomerDAO customerDAO = DAOFactory.getCustomerDAO();
		ArrayList<Customer> customers = (ArrayList<Customer>) customerDAO
				.getCustomersByNameAndOperatorId("zsf", "BY0002");
		Iterator<Customer> it = customers.iterator();
		while (it.hasNext()) {
			Customer customer = it.next();
			String oldName = customer.getName();
			customer.setName(oldName + "["
					+ PinYinUtils.getCapitalizePinYin(oldName) + "]");
			System.out.println(customer);
			Customer customer2=JSONUtils.JSONToCustomer(customer.toString());
			System.out.println("customer from json:");
			System.out.println(customer2);
		}
		
//		Customer customer = new Customer();
//		customer.setAddress("深圳");
//		customer.setAge("43");
//		customer.setCustomerTypeId("2");
//		customer.setIdentityCard("345635249433243456");
//		customer.setMobilePhone("18942564375");
//		customer.setName("赵小兵");
//		customer.setOperatorId("BY0003");
//		customer.setSex("男");
//		customerDAO.add(customer);
		
//		FllowCaseDAO fllowCaseDAO = DAOFactory.getFllowCaseDAO();
//		FllowCase fllowCase = new FllowCase();
//		fllowCase.setCustomerId("2");
//		fllowCase.setOperatorId("BY0001");
//		fllowCase.setRecord("打电话跟进情况");
//		fllowCaseDAO.add(fllowCase);
	}
}
